#include "front/text_normalize.h"
using namespace ppspeech;

const wstring mcWstringDianhuaRegexExample = L"电话|号码|尾号|投诉|创造营";
const wstring mcWstringDianhuaRegexSpace = L"([\\s\\S]*)";
const wstring mcWstringDianhuaRegexNumber = L"(\\d+(-\\d+)?)";

const wregex mcWregexDianhuaHaoma(
	L"(" + mcWstringDianhuaRegexExample + L")" + mcWstringDianhuaRegexSpace + mcWstringDianhuaRegexNumber
	 + L"|"
	 + mcWstringDianhuaRegexNumber + mcWstringDianhuaRegexSpace + L"(" + mcWstringDianhuaRegexExample + L")"
);


//"电话""号码"后面的数字按号码念读
void TextNormalizer::TnReOtherPhone(wstring* fpInWsentence, const LanguageEnum fcInLanguage)
{
	wsmatch tWsmatch;

	while( regex_search(*fpInWsentence, tWsmatch, mcWregexDianhuaHaoma) )
	{
		const wstring tMatch = tWsmatch[0];
		const wchar_t* tp = tMatch.c_str();

		wstring tStrNumber;
		for(int i=0; i<tMatch.length(); i++) {
			if((tp[i] >= L'0' && tp[i] <= L'9') || (tp[i] == L'-')) {
				tStrNumber += tp[i];
			}
			else {
				if(tStrNumber != L"") {
					break;
				}
			}
		}

		wstring tStrReplace = tMatch;
		const vector<string> tItemMobile = StrSplit( wstring2utf8string(tStrNumber), "-");
		const string tReplaceNumber = ReplaceMethod(tItemMobile, (fcInLanguage==LANG_EN)?RME_en_telephone:RME_zh_telephone);

		WstrReplace(tStrReplace, wstring2utf8string(tStrNumber), tReplaceNumber);

		WstrReplacePosition(fpInWsentence, tWsmatch.position(0), tWsmatch.length(0), tStrReplace);
	}
}


const wregex mcWregexMobile(L"(\\d)?((\\+?86 ?)?1([38]\\d|5[0-35-9]|7[678]|9[89])\\d{8})(\\d)?");

// 手机号码，例如：+86 18883862235 --> 八六幺八八八三八六二二三五
void TextNormalizer::TnReMobilePhone(wstring* fpInWsentence, const LanguageEnum fcInLanguage)
{
	wsmatch tWsmatch;

	while( regex_search(*fpInWsentence, tWsmatch, mcWregexMobile) )
	{
		string tStrReplace = "";
		string tStrMobile = wstring2utf8string(tWsmatch[0]);
		if(tStrMobile.length() > 0  &&  tStrMobile[0] == L'+') {
			tStrReplace += "加";
			tStrMobile = tStrMobile.substr(1);
		}

		const vector<string> tItemMobile = StrSplit(tStrMobile, " ");
		tStrReplace += ReplaceMethod(tItemMobile, (fcInLanguage==LANG_EN)?RME_en_telephone:RME_zh_telephone);

		WstrReplacePosition(fpInWsentence, tWsmatch.position(0), tWsmatch.length(0), utf8string2wstring(tStrReplace));
	}
}


const wregex mcWregexPhone(L"(\\d)?((0(10|2[1-3]|[3-9]\\d{2})-?)?[1-9]\\d{6,7})(\\d)?");

// 座机号码，例如：010-51093154 --> 零幺零五幺零九三幺五四
void TextNormalizer::TnReLandlinePhone(wstring* fpInWsentence, const LanguageEnum fcInLanguage)
{
	wsmatch tWsmatch;

	while (regex_search(*fpInWsentence, tWsmatch, mcWregexPhone)) {
		vector<string> tItemPhone = absl::StrSplit(wstring2utf8string(tWsmatch[0]), "-");
		string tStrReplace = ReplaceMethod(tItemPhone, (fcInLanguage==LANG_EN)?RME_en_telephone:RME_zh_telephone);

		WstrReplacePosition(fpInWsentence, tWsmatch.position(0), tWsmatch.length(0), utf8string2wstring(tStrReplace));
	}
}
